package cn.bytecollege.admin.filter;


import cn.bytecollege.admin.service.CategoryService;
import cn.bytecollege.admin.service.CuisineService;
import cn.bytecollege.common.constant.Resource;
import cn.bytecollege.common.entity.Category;
import cn.bytecollege.common.entity.Cuisine;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@WebFilter(filterName = "CuisineFilter",servletNames = {"CuisineUpdateController", "CuisineAddController"})
public class CuisineFilter implements Filter {
    private CategoryService categoryService = null;
    private CuisineService cuisineService = null;
    public CuisineFilter(){
        categoryService = new CategoryService();
        cuisineService = new CuisineService();
    }
    String msg = null;

    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        String uri = req.getRequestURI();
        String method = req.getMethod();
        if(method.equalsIgnoreCase("post")){
            if(uri.contains("cuisineAdd")){
                String cuisineName = req.getParameter("cuisineName");
                String cuisinePrice = req.getParameter("price");
                String category = req.getParameter("category");
                String cuisinePhoto = req.getParameter("photo");
                if(cuisineName == null || cuisineName.equals("")){
                    msg = "菜品名不能为空";
                    List<Category> list = categoryService.find();
                    request.setAttribute("list",list);
                    request.setAttribute("msg",msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/cuisineAdd.jsp")
                            .forward(request,response);
                    return;
                }
                if(cuisinePrice == null || cuisinePrice.equals("")){
                    msg = "菜品价格不能为空";
                    List<Category> list = categoryService.find();
                    request.setAttribute("list",list);
                    request.setAttribute("msg",msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/cuisineAdd.jsp")
                            .forward(request,response);
                    return;
                }
                if(category == null || category.equals("")){
                    msg = "分类不能为空！";
                    List<Category> list = categoryService.find();
                    request.setAttribute("list",list);
                    request.setAttribute("msg",msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/cuisineAdd.jsp")
                            .forward(request,response);
                    return;
                }
                if(cuisinePhoto==null||cuisinePhoto.equals("")){
                    msg = "菜品图片不能为空！";
                    List<Category> list = categoryService.find();
                    request.setAttribute("list",list);
                    request.setAttribute("msg",msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/cuisineAdd.jsp")
                            .forward(request,response);
                    return;
                }

            }else if(uri.contains("cuisineUpdate")){
                String cuisineName = req.getParameter("cuisineName");
                String cuisinePrice = req.getParameter("cuisinePrice");
                String cuisinePhoto = req.getParameter("cuisinePhoto");
                String categoryId = req.getParameter("category");
                String id = req.getParameter("cuisineId");
                if(cuisineName == null || cuisineName.equals("")){
                    msg="菜品名称不能为空！";
                    Cuisine cuisine = cuisineService.findById(Integer.valueOf(id));
                    request.setAttribute("cuisine",cuisine);
                    List<Category> list=categoryService.find();
                    request.setAttribute("list",list);
                    request.setAttribute("msg",msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/cuisineUpdate.jsp").forward(request,response);
                    return;
                }
                if (cuisinePrice==null||cuisinePrice.equals("")){
                    msg="菜品价格不能为空！";
                    List<Category> list=categoryService.find();
                    Cuisine cuisine = cuisineService.findById(Integer.valueOf(id));
                    request.setAttribute("cuisine",cuisine);
                    request.setAttribute("list",list);
                    request.setAttribute("msg",msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/cuisineUpdate.jsp").forward(request,response);
                    return;
                }
                if (cuisinePhoto==null||cuisinePhoto.equals("")){
                    msg="菜品图片不能为空！";
                    List<Category> list=categoryService.find();
                    Cuisine cuisine = cuisineService.findById(Integer.valueOf(id));
                    request.setAttribute("cuisine",cuisine);
                    request.setAttribute("list",list);
                    request.setAttribute("msg",msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/cuisineUpdate.jsp").forward(request,response);
                    return;
                }
                if (categoryId==null||categoryId.equals("")){
                    System.out.println("======================="+categoryId);
                    msg="菜品分类不能为空！";
                    List<Category> list=categoryService.find();
                    Cuisine cuisine = cuisineService.findById(Integer.valueOf(id));
                    request.setAttribute("cuisine",cuisine);
                    request.setAttribute("list",list);
                    request.setAttribute("msg",msg);
                    request.getRequestDispatcher(Resource.PAGE_LOCATION+"/admin/cuisineUpdate.jsp").forward(request,response);
                    return;
                }
                chain.doFilter(request,response);
                return;
            }
        }
        chain.doFilter(request, response);
    }
}
